home *** CD-ROM | disk | FTP | other *** search
/ Internet Info 1994 March / Internet Info CD-ROM (Walnut Creek) (March 1994).iso / answers / news / unix-faq / shell / csh-whynot < prev    next >
Text File  |  1993-10-27  |  16KB  |  558 lines

  1. Newsgroups: comp.unix.shell,comp.unix.questions,comp.unix.programmer,news.answers
  2. Path: senator-bedfellow.mit.edu!bloom-beacon.mit.edu!news.kei.com!sol.ctr.columbia.edu!howland.reston.ans.net!agate!boulder!wraeththu.cs.colorado.edu!tchrist
  3. From: Tom Christiansen <tchrist@cs.Colorado.EDU>
  4. Subject: Csh Programming Considered Harmful 
  5. Message-ID: <CFK3E8.1p8@Colorado.EDU>
  6. Followup-To: comp.unix.shell
  7. Originator: tchrist@wraeththu.cs.colorado.edu
  8. Sender: news@Colorado.EDU (USENET News System)
  9. Organization: University of Colorado at Boulder
  10. Date: Wed, 27 Oct 1993 12:44:30 GMT
  11. Approved: news-answers-request@MIT.Edu
  12. Expires: Wed, 1 Dec 1993 12:00:00 GMT
  13. Lines: 542
  14. Xref: senator-bedfellow.mit.edu comp.unix.shell:13174 comp.unix.questions:63765 comp.unix.programmer:14228 news.answers:13980
  15.  
  16. Archive-name: unix-faq/shell/csh-whynot
  17. Version: $Id: csh-faq,v 1.5 93/10/01 13:26:05 tchrist Exp Locker: tchrist $
  18.  
  19. The following periodic article answers in excruciating detail
  20. the frequently asked question "Why shouldn't I program in csh?".
  21. It is available for anon FTP from convex.com in /pub/csh.whynot .
  22.  
  23.  
  24.            *** CSH PROGRAMMING CONSIDERED HARMFUL ***
  25.  
  26.     Resolved: The csh is a tool utterly inadequate for programming, 
  27.           and its use for such purposes should be strictly banned!
  28.  
  29. I am continually shocked and dismayed to see people write test cases,
  30. install scripts, and other random hackery using the csh.  Lack of
  31. proficiency in the Bourne shell has been known to cause errors in /etc/rc
  32. and .cronrc files, which is a problem, because you *must* write these files
  33. in that language.
  34.  
  35. The csh is seductive because the conditionals are more C-like, so the path
  36. of least resistance is chosen and a csh script is written.  Sadly, this is
  37. a lost cause, and the programmer seldom even realizes it, even when they
  38. find that many simple things they wish to do range from cumbersome to
  39. impossible in the csh.
  40.  
  41.  
  42. 1. FILE DESCRIPTORS
  43.  
  44. The most common problem encountered in csh programming is that
  45. you can't do file-descriptor manipulation.  All you are able to 
  46. do is redirect stdin, or stdout, or dup stderr into stdout. 
  47. Bourne-compatible shells offer you an abundance of more exotic
  48. possibilities.    
  49.  
  50. 1a. Writing Files
  51.  
  52. In the Bourne shell, you can open or dup arbitrary file descriptors.
  53. For example, 
  54.  
  55.     exec 2>errs.out
  56.  
  57. means that from then on, stderr goes into errs file.
  58.  
  59. Or what if you just want to throw away stderr and leave stdout
  60. alone?    Pretty simple operation, eh?
  61.  
  62.     cmd 2>/dev/null
  63.  
  64. Works in the Bourne shell.  In the csh, you can only make a pitiful 
  65. attempt like this:
  66.  
  67.     (cmd > /dev/tty) >& /dev/null
  68.  
  69. But who said that stdout was my tty?  So it's wrong.  This simple
  70. operation *CANNOT BE DONE* in the csh.
  71.  
  72.  
  73. Along these same lines, you can't direct error messages in csh scripts
  74. out stderr as is considered proper.  In the Bourne shell, you might say:
  75.  
  76.     echo "$0: cannot find $file" 1>&2
  77.  
  78. but in the csh, you can't redirect stdout out stderr, so you end
  79. up doing something silly like this:
  80.  
  81.     sh -c 'echo "$0: cannot find $file" 1>&2'
  82.  
  83. 1b. Reading Files
  84.  
  85. In the csh, all you've got is $<, which reads a line from your tty.  What
  86. if you've redirected stdin?  Tough noogies, you still get your tty, which 
  87. you really can't redirect.  Now, the read statement 
  88. in the Bourne shell allows you to read from stdin, which catches
  89. redirection.  It also means that you can do things like this:
  90.  
  91.     exec 3<file1
  92.     exec 4<file2
  93.  
  94. Now you can read from fd 3 and get lines from file1, or from file2 through
  95. fd 4.   In modern, Bourne-like shells, this suffices: 
  96.  
  97.     read some_var 0<&3
  98.     read another_var 0<&4
  99.  
  100. Although in older ones where read only goes from 0, you trick it:
  101.  
  102.     exec 5<&0  # save old stdin
  103.     exec 0<&3; read some_var
  104.     exec 0<&4; read another_var
  105.     exec 0<&5  # restore it
  106.  
  107.  
  108. 1c. Closing FDs
  109.  
  110. In the Bourne shell, you can close file descriptors you don't
  111. want open, like 2>&-, which isn't the same as redirecting it
  112. to /dev/null.
  113.  
  114. 1d. More Elaborate Combinations
  115.  
  116. Maybe you want to pipe stderr to a command and leave stdout alone.
  117. Not too hard an idea, right?  You can't do this in the csh as I
  118. mentioned in 1a.  In a Bourne shell, you can do things like this:
  119.  
  120.     exec 3>&1; grep yyy xxx 2>&1 1>&3 3>&- | sed s/file/foobar/ 1>&2 3>&-
  121.     grep: xxx: No such foobar or directory
  122.  
  123. Normal output would be unaffected.  The closes there were in case
  124. something really cared about all its FDs.  We send stderr to sed,
  125. and then put it back out 2.
  126.  
  127. Consider the pipeline:
  128.  
  129.     A | B | C
  130.  
  131. You want to know the status of C, well, that's easy: it's in $?, or
  132. $status in csh.  But if you want it from A, you're out of luck -- if
  133. you're in the csh, that is.  In the Bourne shell, you can get it, although
  134. doing so is a bit tricky.  Here's something I had to do where I ran dd's
  135. stderr into a grep -v pipe to get rid of the records in/out noise, but had
  136. to return the dd's exit status, not the grep's:
  137.  
  138.     device=/dev/rmt8
  139.     dd_noise='^[0-9]+\+[0-9]+ records (in|out)$'
  140.     exec 3>&1
  141.     status=`((dd if=$device ibs=64k 2>&1 1>&3 3>&- 4>&-; echo $? >&4) |
  142.         egrep -v "$dd_noise" 1>&2 3>&- 4>&-) 4>&1`
  143.     exit $status;
  144.  
  145.  
  146. The csh has also been known to close all open file descriptors besides
  147. the ones it knows about, making it unsuitable for applications that 
  148. intend to inherit open file descriptors.
  149.  
  150.  
  151. 2. COMMAND ORTHOGONALITY
  152.  
  153. 2a. Built-ins
  154.  
  155. The csh is a horrid botch with its built-ins.  You can't put them
  156. together in many reasonable ways.   Even simple little things like this:    
  157.  
  158.         % time | echo
  159.  
  160. which while nonsensical, shouldn't give me this message:
  161.  
  162.         Reset tty pgrp from 9341 to 26678
  163.  
  164. Others are more fun:
  165.  
  166.         % sleep 1 | while
  167.         while: Too few arguments.
  168.         [5] 9402
  169.         % jobs
  170.         [5]     9402 Done                 sleep |
  171.  
  172.  
  173. Some can even hang your shell.  Try typing ^Z while you're sourcing 
  174. something, or redirecting a source command.  Just make sure you have
  175. another window handy.  Or try 
  176.  
  177.     % history | more
  178.  
  179. on some systems.
  180.  
  181. Aliases are not evaluated everywhere you would like them do be:
  182.  
  183.     % alias lu 'ls -u'
  184.     % lu
  185.     HISTORY  News     bin      fortran  lib      lyrics   misc     tex
  186.     Mail     TEX      dehnung  hpview   logs     mbox     netlib
  187.     % repeat 3 lu
  188.     lu: Command not found.
  189.     lu: Command not found.
  190.     lu: Command not found.
  191.  
  192.     % time lu
  193.     lu: Command not found.
  194.  
  195.  
  196. 2b. Flow control
  197.  
  198. You can't mix flow-control and commands, like this:
  199.     
  200.     who | while read line; do
  201.     echo "gotta $line"
  202.     done
  203.  
  204.  
  205. You can't combine multiline constructs in a csh using semicolons.
  206. There's no easy way to do this
  207.  
  208.     alias cmd 'if (foo) then bar; else snark; endif'
  209.  
  210.  
  211. You can't perform redirections with if statements that are
  212. evaluated solely for their exit status:
  213.  
  214.     if ( { grep vt100 /etc/termcap > /dev/null } ) echo ok
  215.  
  216. And even pipes don't work:
  217.  
  218.     if ( { grep vt100 /etc/termcap | sed 's/$/###' } ) echo ok
  219.  
  220. But these work just fine in the Bourne shell:
  221.  
  222.     if grep vt100 /etc/termcap > /dev/null ; then echo ok; fi   
  223.  
  224.     if grep vt100 /etc/termcap | sed 's/$/###/' ; then echo ok; fi
  225.  
  226.  
  227. Consider the following reasonable construct:
  228.  
  229.   if ( { command1 | command2 } ) then
  230.       ...
  231.   endif
  232.  
  233. The output of command1 won't go into the input of command2.  You will get
  234. the output of both commands on standard output.  No error is raised.  In
  235. the Bourne shell or its clones, you would say 
  236.  
  237.     if command1 | command2 ; then
  238.     ...
  239.     fi
  240.  
  241.  
  242. 2c. Stupid parsing bugs
  243.  
  244. Certain reasonable things just don't work, like this:
  245.  
  246.     % kill -1 `cat foo`
  247.     `cat foo`: Ambiguous.
  248.  
  249. But this is ok:
  250.  
  251.     % /bin/kill -1 `cat foo`
  252.  
  253. If you have a stopped job:
  254.  
  255.     [2]     Stopped              rlogin globhost
  256.  
  257. You should be able to kill it with 
  258.  
  259.     % kill %?glob
  260.     kill: No match
  261.  
  262. but
  263.  
  264.     % fg %?glob
  265.  
  266. works.
  267.  
  268. White space can matter:
  269.  
  270.     if(expr)
  271.  
  272. may fail on some versions of csh, while
  273.  
  274.     if (expr)
  275.  
  276. works!
  277.  
  278.  
  279.  
  280. 3. SIGNALS
  281.  
  282. In the csh, all you can do with signals is trap SIGINT.  In the Bourne
  283. shell, you can trap any signal, or the end-of-program exit.    For example,
  284. to blow away a tempfile on any of a variety of signals:
  285.  
  286.     $ trap 'rm -f /usr/adm/tmp/i$$ ;
  287.         echo "ERROR: abnormal exit";
  288.         exit' 1 2 3 15
  289.  
  290.     $ trap 'rm tmp.$$' 0   # on program exit
  291.  
  292.  
  293.  
  294. 4. QUOTING
  295.  
  296. You can't quote things reasonably in the csh:
  297.  
  298.     set foo = "Bill asked, \"How's tricks?\""
  299.  
  300. doesn't work.  This makes it really hard to construct strings with
  301. mixed quotes in them.  In the Bourne shell, this works just fine. 
  302. In fact, so does this:
  303.  
  304.      cd /mnt; /usr/ucb/finger -m -s `ls \`u\``
  305.  
  306. Dollar signs cannot be escaped in double quotes in the csh.  Ug.
  307.  
  308.     set foo = "this is a \$dollar quoted and this is $HOME not quoted" 
  309.     dollar: Undefined variable.
  310.  
  311. You have to use backslashes for newlines, and it's just darn hard to
  312. get them into strings sometimes.
  313.  
  314.     set foo = "this \
  315.     and that";
  316.     echo $foo
  317.     this  and that
  318.     echo "$foo"
  319.     Unmatched ".  
  320.  
  321. Say what?  You don't have these problems in the Bourne shell, where it's
  322. just fine to write things like this:
  323.  
  324.     echo     'This is 
  325.          some text that contains
  326.          several newlines.'
  327.  
  328.  
  329. As distributed, quoting history references is a challenge.  Consider:
  330.  
  331.     % mail adec23!alberta!pixel.Convex.COM!tchrist
  332.     alberta!pixel.Convex.COM!tchri: Event not found.
  333.  
  334.  
  335. 5. VARIABLE SYNTAX
  336.  
  337. There's this big difference between global (environment) and local
  338. (shell) variables.  In csh, you use a totally different syntax 
  339. to set one from the other.  
  340.  
  341. In the Bourne shell, this 
  342.     VAR=foo cmds args
  343.  is the same as
  344.     (export VAR; VAR=foo; cmd args)
  345. or csh's
  346.     (setenv VAR;  cmd args)
  347.  
  348. You can't use :t, :h, etc on envariables.  Watch:
  349.     echo Try testing with $SHELL:t
  350.  
  351. It's really nice to be able to say
  352.     
  353.     ${PAGER-more}
  354. or
  355.     FOO=${BAR:-${BAZ}}
  356.  
  357. to be able to run the user's PAGER if set, and more otherwise.
  358. You can't do this in the csh.  It takes more verbiage.
  359.  
  360. You can't get the process number of the last background command from the
  361. csh, something you might like to do if you're starting up several jobs in
  362. the background.  In the Bourne shell, the pid of the last command put in
  363. the background is available in $!.
  364.  
  365. The csh is also flaky about what it does when it imports an 
  366. environment variable into a local shell variable, as it does
  367. with HOME, USER, PATH, and TERM.  Consider this:
  368.  
  369.     % setenv TERM '`/bin/ls -l / > /dev/tty`'
  370.     % csh -f
  371.  
  372. And watch the fun!
  373.  
  374.  
  375. 6. EXPRESSION EVALUATION
  376.  
  377. Consider this statement in the csh:
  378.  
  379.  
  380.     if ($?MANPAGER) setenv PAGER $MANPAGER
  381.  
  382.  
  383. Despite your attempts to only set PAGER when you want
  384. to, the csh aborts:
  385.  
  386.     MANPAGER: Undefined variable.
  387.  
  388. That's because it parses the whole line anyway AND EVALUATES IT!
  389. You have to write this:
  390.  
  391.     if ($?MANPAGER) then
  392.     setenv PAGER $MANPAGER
  393.     endif
  394.  
  395. That's the same problem you have here:
  396.  
  397.     if ($?X && $X == 'foo') echo ok
  398.     X: Undefined variable
  399.  
  400. This forces you to write a couple nested if statements.  This is highly
  401. undesirable because it renders short-circuit booleans useless in
  402. situations like these.  If the csh were the really C-like, you would
  403. expect to be able to safely employ this kind of logic.  Consider the
  404. common C construct:
  405.  
  406.     if (p && p->member) 
  407.  
  408. Undefined variables are not fatal errors in the Bourne shell, so 
  409. this issue does not arise there.
  410.  
  411. While the csh does have built-in expression handling, it's not
  412. what you might think.  In fact, it's space sensitive.  This is an
  413. error
  414.  
  415.    @ a = 4/2
  416.  
  417. but this is ok
  418.  
  419.    @ a = 4 / 2
  420.  
  421.  
  422. The ad hoc parsing csh employs fouls you up in other places 
  423. as well.  Consider:
  424.  
  425.     % alias foo 'echo hi' ; foo
  426.     foo: Command not found.
  427.     % foo
  428.     hi
  429.  
  430.  
  431.  
  432. 7. ERROR HANDLING
  433.  
  434. Wouldn't it be nice to know you had an error in your script before
  435. you ran it?   That's what the -n flag is for: just check the syntax.
  436. This is especially good to make sure seldom taken segments of code
  437. code are correct.  Alas, the csh implementation of this doesn't work.
  438. Consider this statement:
  439.  
  440.     exit (i)
  441.  
  442. Of course, they really meant
  443.  
  444.     exit (1)
  445.  
  446. or just
  447.  
  448.     exit 1
  449.  
  450. Either shell will complain about this.  But if you hide this in an if
  451. clause, like so:
  452.  
  453.     #!/bin/csh -fn
  454.     if (1) then
  455.     exit (i)
  456.     endif
  457.  
  458. The csh tells you there's nothing wrong with this script.  The equivalent
  459. construct in the Bourne shell, on the other hand, tells you this:
  460.  
  461.  
  462.     #!/bin/sh -n
  463.     if (1) then
  464.     exit (i)
  465.     endif
  466.  
  467.     /tmp/x: syntax error at line 3: `(' unexpected
  468.  
  469.  
  470.  
  471. RANDOM BUGS
  472.  
  473. Here's one:
  474.  
  475.     fg %?string
  476.     ^Z
  477.     kill  %?string
  478.     No match.
  479.  
  480. Huh? Here's another
  481.  
  482.     !%s%x%s
  483.  
  484. Coredump, or garbage.
  485.  
  486. If you have an alias with backquotes, and use that in backquotes in 
  487. another one, you get a coredump.
  488.  
  489. Try this:
  490.     % repeat 3 echo "/vmu*"
  491.     /vmu*
  492.     /vmunix
  493.     /vmunix
  494. What???
  495.  
  496.  
  497. Here's another one:
  498.  
  499.     % mkdir tst
  500.     % cd tst
  501.     % touch '[foo]bar'
  502.     % foreach var ( * )
  503.     > echo "File named $var"
  504.     > end
  505.     foreach: No match.
  506.  
  507.  
  508. 8. SUMMARY
  509.  
  510.  
  511. While some vendors have fixed some of the csh's bugs (the tcsh also does
  512. much better here), many have added new ones.  Most of its problems can
  513. never be solved because they're not actually bugs per se, but rather the
  514. direct consequences of braindead design decisions.  It's inherently flawed.
  515.  
  516. Do yourself a favor, and if you *have* to write a shell script, do it in the 
  517. Bourne shell.  It's on every UNIX system out there.  However, behavior 
  518. can vary.
  519.  
  520. There are other possibilities.
  521.  
  522. The Korn shell is the preferred programming shell by many sh addicts,
  523. but it still suffers from inherent problems in the Bourne shell's design,
  524. such as parsing and evaluation horrors.  The Korn shell or its
  525. public-domain clones and supersets (like bash) aren't quite so ubiquitous
  526. as sh, so it probably wouldn't be wise to write a sharchive in them that
  527. you post to the net.  When 1003.2 becomes a real standard that companies
  528. are forced to adhere to, then we'll be in much better shape.  Until
  529. then, we'll be stuck with bug-incompatible versions of the sh lying about.
  530.  
  531. The Plan 9 shell, rc, is much cleaner in its parsing and evaluation; it is
  532. not widely available, so you'd be significantly sacrificing portability.
  533. No vendor is shipping it yet.
  534.  
  535. If you don't have to use a shell, but just want an interpreted language,
  536. many other free possibilities present themselves, like Perl, REXX, TCL,
  537. Scheme, or Python.  Of these, Perl is probably the most widely available
  538. on UNIX (and many other) systems and certainly comes with the most
  539. extensive UNIX interface.  Increasing numbers vendors ship Perl with 
  540. their standard systems.  (See the comp.lang.perl FAQ for a list.)
  541.  
  542. If you have a problem that would ordinarily use sed or awk or sh, but it
  543. exceeds their capabilities or must run a little faster, and you don't want
  544. to write the silly thing in C, then Perl may be for you.  You can get
  545. at networking functions, binary data, and most of the C library. There
  546. are also translators to turn your sed and awk scripts into Perl scripts,
  547. as well as a symbolic debugger.  Tchrist's personal rule of thumb is
  548. that if it's the size that fits in a Makefile, it gets written in the
  549. Bourne shell, but anything bigger gets written in Perl.
  550.  
  551. See the comp.lang.{perl,rexx,tcl} newsgroups for details about these
  552. languages (including FAQs), or David Muir Sharnoff's comparison of 
  553. freely available languages and tools in comp.lang.misc and news.answers.
  554. -- 
  555.     Tom Christiansen      tchrist@cs.colorado.edu       
  556.       "Will Hack Perl for Fine Food and Fun"
  557.     Boulder Colorado  303-444-3212
  558.